package com.machine.service.user.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.machine.bean.BuyComponent;
import com.machine.bean.Componentpart;
import com.machine.bean.PageBean;
import com.machine.dao.user.BuyComponentDao;
import com.machine.service.user.BuyComponentServiceDao;

@Component(value = "buyComponentService")
public class BuyComponentServiceDaoImpl implements BuyComponentServiceDao {

	private BuyComponentDao buyComponentDao;

	public BuyComponentDao getBuyComponentDao() {
		return buyComponentDao;
	}

	@Resource(name = "buyComponentDao")
	public void setBuyComponentDao(BuyComponentDao buyComponentDao) {
		this.buyComponentDao = buyComponentDao;
	}

	@Override
	public int addBuyCom(BuyComponent buyComponent) {
		return buyComponentDao.addBuyCom(buyComponent);
	}

	@Override
	public PageBean queryForPage(int pageSize, int page, String username) {

		final String hql = "from BuyComponent where buyname='" + username + "'"; // 查询语句
		int allRow = buyComponentDao.getAllRowCount(hql); // 总记录数

		int totalPage = PageBean.countTotalPage(pageSize, allRow); // 总页数
		final int offset = PageBean.countOffset(pageSize, page); // 当前页开始记录
		final int length = pageSize; // 每页记录数
		final int currentPage = PageBean.countCurrentPage(page);
		List<Componentpart> list = buyComponentDao.queryForPage(hql, offset,
				length); // "一页"的记录
		// 把分页信息保存到Bean中
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		System.out.println("allrow : " + allRow);
		System.out.println("totalPage : " + totalPage);
		System.out.println("offset : " + offset);
		System.out.println("currentPage : " + currentPage);
		return pageBean;
	}

	@Override
	public PageBean queryForPage(int pageSize, int page) {

		final String hql = "from BuyComponent"; // 查询语句
		int allRow = buyComponentDao.getAllRowCount(hql); // 总记录数

		int totalPage = PageBean.countTotalPage(pageSize, allRow); // 总页数
		final int offset = PageBean.countOffset(pageSize, page); // 当前页开始记录
		final int length = pageSize; // 每页记录数
		final int currentPage = PageBean.countCurrentPage(page);
		List<Componentpart> list = buyComponentDao.queryForPage(hql, offset,
				length); // "一页"的记录
		// 把分页信息保存到Bean中
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		return pageBean;
	}

	@Override
	public int deleteByIdList(List<Integer> idList) {
		return buyComponentDao.deleteByIdList(idList);
	}

	@Override
	public BuyComponent findById(Integer id) {
		return buyComponentDao.findById(id);
	}

	@Override
	public int update(BuyComponent buyComponent) {
		
		return buyComponentDao.update(buyComponent);
	}

}
